package cc.ufinity.networkdiagnose.adapter;

import java.util.List;

import cc.ufinity.networkdiagnose.manager.City;
import cc.ufinity.networkdiagnose.view.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CityiListAdapter extends BaseAdapter {

	private Context mContext;
	private List<City> mCitys;
	
	// ���ز���
	LayoutInflater vi;
	
	public CityiListAdapter(Context context, List<City> citys)
	{
		mContext = context;
		mCitys = citys;
	}
	
	@Override
	public int getCount() {
		
		if(mCitys == null)
			return 0;
		return mCitys.size();
	}
	
	@Override
	public Object getItem(int position) {
		if(mCitys == null)
			return null;
		else
			return mCitys.get(position);
	}
	
	@Override
	public long getItemId(int position) {
		
		return position;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		View view;
		
		if(convertView == null)
		{
			vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = (LinearLayout) vi.inflate(R.layout.city_list_item,null);
		} else {
			view = convertView;
		}
		
		FillDataIntoView(view,position);
		
		return view;
	}
	
	public void FillDataIntoView(View view, int position) {
		final City city = mCitys.get(position);
		
		ImageView iv = (ImageView)view.findViewById(R.id.hot_img);
		TextView tv = (TextView)view.findViewById(R.id.hot_name);

		tv.setText(city.getName());
		if(city.isSelected()) {
			iv.setBackgroundResource(R.drawable.checkbox_1);
		} else {
			iv.setBackgroundResource(R.drawable.checkbox_2);
		}
	}

}
